package cn.neusoft.controller;

import cn.neusoft.pojo.Address;
import cn.neusoft.pojo.CartItem;
import cn.neusoft.pojo.Order;
import cn.neusoft.pojo.User;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.BusinessQuery;
import cn.neusoft.service.*;
import cn.neusoft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import javax.xml.transform.Result;
import java.sql.SQLException;

@RestController
@RequestMapping("/app")
public class AppController {

	@Autowired
	IAppService appService;
	@Autowired
	IBusinessService businessService;
	@Autowired
	IFoodService foodService;
	@Autowired
	ICartItemService cartItemService;
	@Autowired
	IOrderService orderService;
	@Autowired
	IAddressService addressService;
	@Autowired
	IUserService userService;
	//查询推荐商家
	@GetMapping("/business/recommend")   //   get  +  /app/business/recommend
	public ResultBean getRecommendBusiness() throws SQLException {
		System.out.println("-------AppController-----getRecommendBusiness--------");
		return appService.getRecommendBusiness();
	}
	//分页进行查询商家
	@PostMapping("/business/page")
	public ResultBean getByPage(@RequestBody BusinessQuery businessQuery) throws Exception {
		System.out.println("-------AppController-----getByPage--------");
		System.out.println(businessQuery);
		return businessService.getByPage(businessQuery);
	}
	@GetMapping("/food/business/{bid}")
	public ResultBean getFoodByBid(@PathVariable("bid") Integer bid) {
		System.out.println("-------AppController-----getFoodByBid--------");
		return foodService.getBusinessId(bid);
	}
	//先展示商家的信息
	@GetMapping("/business/{id}")
	public ResultBean getBusiness(@PathVariable("id") Integer id) throws Exception {
		System.out.println("-------AppController-----getBusiness--------");
		System.out.println("该商家id为："+id);
		return businessService.getById(id);
	}
	//商家显示有了，接下来就是添加商品到购物车里
	@PostMapping("/cart/save")
	public ResultBean saveCartItem(@RequestBody CartItem cartItem) throws Exception {
		System.out.println("-------AppController-----saveCartItem--------");
		if(cartItem.getCartId()==null) {
			return cartItemService.add(cartItem);
		}
		return cartItemService.update(cartItem);
	}
	//某个用户在某个商家的购物车内容
	@GetMapping("/cart/{userId}/{businessId}")
	public ResultBean getCartByBidAndUid(@PathVariable("userId") Integer userId,
										 @PathVariable("businessId") Integer businessId) {
		System.out.println("------AppController---------getCartByBidAndUid----------------");
		return cartItemService.getCartByBidAndUid(userId,businessId);
	}
	@DeleteMapping("/cartItem/{id}")
	public ResultBean deleteCartItem(@PathVariable("id") Integer id) throws Exception {
		System.out.println("------AppController---------deleteCartItem----------------");
		System.out.println("删除的购物车id为："+id);
		return cartItemService.delete(id);
	}
	@PostMapping("/order/add")     //  请求方式 + 请求路径   post  /app/order/add
	public ResultBean addOrder(@RequestBody Order order) throws Exception {
		System.out.println("----AppController-----add--------");
		System.out.println(order);
		return orderService.add(order);
	}
	@PutMapping("/order/update")
	public ResultBean upddate(@RequestBody Order order) throws Exception {
		System.out.println("----OrderController-----update--------");
		return orderService.update(order);
	}
	@GetMapping("/address/user/{userId}")
	public ResultBean getAddressByUserId(@PathVariable("userId") Integer userId) {
		System.out.println("----AppController-----getAddressByUserId--------");
		System.out.println("用户的id为："+userId);
		return addressService.getByUserId(userId);
	}
	@PostMapping("/address/save")  //  请求方式 + 请求路径   post  /address
	public ResultBean save(@RequestBody Address address) throws Exception {
		System.out.println("----AddressController-----add--------");
		System.out.println(address);
		if(address.getAddressId()==null) {
			System.out.println("================");
			return addressService.add(address);
		}
		return  addressService.update(address);
	}
	//  get   /address/1004
	@GetMapping("/address/{id}")   // get /app/address/1003
	public ResultBean getAddressById(@PathVariable("id") Integer id) throws Exception {
		System.out.println("-------AppController--------getById-------------");
		System.out.println(id);
		return addressService.getById(id);
	}
	//根据用户id来查询订单
	@GetMapping("/order/user/{userId}")
	public ResultBean getOrderByUserId(@PathVariable("userId") Integer userId) {
		System.out.println("-------AppController--------getOrderByUserId-------------");
		System.out.println("该用户下的订单编号为："+userId);
		return orderService.getOrderByUserId(userId);
	}
	@GetMapping("/order/detail/{orderId}")
	public ResultBean getDetailByOrderId(@PathVariable("orderId") Integer orderId) {
		System.out.println("-------AppController--------getDetailByOrderId-------------");
		System.out.println("该用户下的订单编号为："+orderId);
		return orderService.getDetailsByOrderId(orderId);
	}
	//拿取用户的信息
	@GetMapping("/user/{id}")
	public ResultBean getById (@PathVariable("id") Integer id) throws Exception {
		System.out.println("----UserController-----getById--------");
		return userService.getById(id);
	}
	@PostMapping("/login")
	public ResultBean login(HttpSession session, @RequestBody User user) throws Exception {
		System.out.println("-------AppController-----login--------");
		System.out.println(user.getUsername()+" "+user.getPassword());
		ResultBean resultBean=userService.login(user.getUsername(),user.getPassword());
		if(resultBean.getCode()==200) {
			//说明成功登录，分配id
			System.out.println("----登录成功----"+session.getId());
			session.setAttribute("loginUser",user);
		}
		return resultBean;
	}
}